package Firepear::Carrot::Handler::Text;
use base 'Firepear::Carrot::Handler';

use warnings;
use strict;

=head1 NAME

Firepear::Carrot::Handler::Text - Universal text handler

=head1 VERSION

Version 1.000

=cut

our $VERSION = '1.000';


=head1 SYNOPSIS

This module contains the custom code for assembling text chunks.

=head1 PARSING METHODS

=head2 lex

=cut

sub lex {
    my ($self,$l) = @_;
    $self->clearnode;
    $self->get_nodeinfo($l);

    my $token = $l->current;
    my $content = $token;

    until($self->status or $l->status) {
	$token = $l->fetch;

	# para breaks
	last if ($l->current eq "\n" and $l->prev eq "\n");
	# handle comments
	$token = $self->commenter($l,$self->{mu}{r});
	next if $token eq '';
	# handle possible markup
	$token = $self->endnode($l);
	# add to content chunk
	$content = join('',$content,$token);
    }
    $self->status(1);
    $self->{node}{content} = $content;
    return $self->{node};
}

=head2 endnode

If B<any> markup delineators are found, a status is set, the token is
split at the markup, anything before the markup is set as the Lexer's
current token, remainder is set as the next token.

Either case terminates text node processing.

=cut

sub endnode {
    my ($self,$l) = @_;
    my $tok = $l->current;

    # markup
    my $o = $self->{mu}{z}{o};
    my $c = $self->{mu}{z}{c};

    if ($tok =~ /[$o$c]/) {
	unless ($tok =~ /$o.{1,2}$c/) { # entities ok
	    my @chunks = split /([$o$c]+)/, $tok;
	    $l->settoken(cur => $chunks[0],
			 next => join("", @chunks[1..@chunks-1]));
	    $self->status(1);
	    return $chunks[0];
	}
    }
    return $tok;
}

=head2 get_nodeinfo

Performs initial assembly of the C<node> construct. Sets the node
type, and gets file info from the Lexer.

=cut

sub get_nodeinfo {
    my ($self,$l) = @_;
    my $fileinfo = $l->fileinfo;
    $self->{node}{finfo} = [ $fileinfo->{fnum}, $fileinfo->{lnum} ];
    $self->{node}{type}  = $self->{type};
}

=head2 setmarkup_local

Performs handler-specific markup initialization.

=cut

sub setmarkup_local {
    my ($self, $mu, $markup) = @_;

    $markup->{o} = undef; $markup->{c} = undef;
}


=head1 AUTHOR

Shawn Boyette, C<< <mdxi@cpan.org> >>

=head1 BUGS

Please report any bugs or feature requests at
L<http://carrot.googlecode.com>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 COPYRIGHT & LICENSE

Copyright 2008 Shawn Boyette

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of the <ORGANIZATION> nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=cut

1; # End of Firepear::Carrot::Handler::Trigger
